package com.example.easyexcel.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.fastjson.JSON;
import com.example.easyexcel.model.Address;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author dangwen1
 * @date 2021年12月02日 21:19
 * @description
 */
public class AddressListConverter implements Converter<List<Address>> {
    @Override
    public Class<List<Address>> supportJavaTypeKey() {
        return null;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public List<Address> convertToJavaData(CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        return null;
    }

    @Override
    public CellData<String> convertToExcelData(List<Address> addresses, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        List<String> collect = addresses.stream().map(x -> {
            String provinceName = x.getProvinceName();
            String cityName = x.getCityName();
            return provinceName + "-" + cityName;
        }).collect(Collectors.toList());
        return new CellData<>(JSON.toJSONString(collect).replace("\"","").replace("[","").replace("]",""));
    }
}
